﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using psafth.AMB;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.Specialized;

namespace psafth.RCTiming.Main
{
    [Serializable]
    public class Lap : INotifyPropertyChanged
    {
        private double _Laptime;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lt">Laptime</param>
        /// <param name="ts">Timestamp</param>
        /// <param name="ss">Signalstrength</param>
        public Lap(double lt, DateTime ts, double ss, double vo, double hs)
        {
            this.LapTime = lt;
            this.PassingTime = ts;
            this.SignalStrength = ss;
            this.Hits = hs;
            this.IsValid = true;
            this.InvalidReason = InvalidationType.NONE;
            this.Voltage = vo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lt">Laptime</param>
        /// <param name="ts">Timestamp</param>
        /// <param name="ss">Signalstrength</param>
        /// <param name="manual">Manually counted?</param>
        public Lap(double lt, DateTime ts, double ss, double vo, double hs, bool manual)
        {
            this.LapTime = lt;
            this.PassingTime = ts;
            this.SignalStrength = ss;
            this.Hits = hs;
            this.ManualCount = manual;
            this.Voltage = vo;
            this.IsValid = true;
            this.InvalidReason = InvalidationType.NONE;
        }

        public int Id { get; set; }

        public double LapTime
        {
            get
            {
                return AMBCalculator.TrimDouble(_Laptime, 3);
            }
            private set
            {
                this._Laptime = value;
            }
        }

        public bool ManualCount
        {
            get;
            set;
        }

        public bool IsValid
        {
            get;
            set;
        }

        public InvalidationType InvalidReason
        {
            get;
            set;
        }

        public double Voltage
        {
            get;
            set;
        }

        public double SignalStrength
        {
            get;
            set;
        }

        public double Hits
        {
            get;
            set;
        }

        public DateTime PassingTime
        {
            get;
            set;
        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public enum InvalidationType
        {
            NONE = 0,
            PENALTY = 1,
            CORRECTION = 2,
        }
    }
}
